package com.wind.adapter;

import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;


import com.bumptech.glide.Glide;
import com.wind.beans.kindBean;
import com.debug.soblogin.R;
import com.wind.soblogin.DebugApplication;

import java.util.List;

public abstract class RecycleViewBaseAdapter extends RecyclerView.Adapter<RecycleViewBaseAdapter.InnerHolder> {
    private final List<kindBean> mData;
    private OnItemClickListener mOnItemClickListener;

    public RecycleViewBaseAdapter(List<kindBean> data) {
        this.mData = data;
    }

    @NonNull
    @Override
    public InnerHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = getSubView(parent, viewType);
        return new InnerHolder(view);
    }

    protected abstract View getSubView(ViewGroup parent, int viewType);


    @Override
    public void onBindViewHolder(@NonNull InnerHolder holder, int position) {
        holder.setData(mData.get(position), position);
    }

    @Override
    public int getItemCount() {
        if (mData != null) {
            return mData.size();
        }
        return 0;
    }

    public void setOnItemClickListener(OnItemClickListener listener) {
        //设置一个监听，就是设置一个接口，一个回调接口
        this.mOnItemClickListener = listener;

    }

    public interface OnItemClickListener {
        void onItemClick(int position);
    }


    public class InnerHolder extends RecyclerView.ViewHolder {
        private final TextView mTitle;
        private final ImageView mDes;
        private int mPosition;

        public InnerHolder(@NonNull View itemView) {
            super(itemView);
            mTitle = itemView.findViewById(R.id.diary_title);
            mDes = itemView.findViewById(R.id.image);

            itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (mOnItemClickListener != null) {
                        mOnItemClickListener.onItemClick(mPosition);
                    }
                }
            });
        }


        public void setData(kindBean kb, int position) {
            this.mPosition = position;
            mTitle.setText(kb.url);
            Glide.with(DebugApplication.getContext()).load(kb.url).into(mDes);
        }
    }
}
